#include "campaignform.h"
#include "ui_campaignform.h"


#include <fileactions.h>


#include <QAction>
#include <QTimer>
#include <QDebug>


CampaignForm::CampaignForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CampaignForm),
    m_fileName(),
    m_campaign(FileActions::create(tr("New Campaign"), this)),
    m_model(new DTwentyModel(m_campaign, this))
{
    ui->setupUi(this);
    ui->treeView->setModel(m_model);

    addActions(findChildren<QAction*>(QString(), Qt::FindDirectChildrenOnly));



}

CampaignForm::CampaignForm(const QString &campaignFileName, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CampaignForm),
    m_fileName(campaignFileName),
    m_campaign(FileActions::open(m_fileName, this)),
    m_model(new DTwentyModel(m_campaign, this))
{
    ui->setupUi(this);
    ui->treeView->setModel(m_model);

    addActions(findChildren<QAction*>(QString(), Qt::FindDirectChildrenOnly));
}

CampaignForm::~CampaignForm()
{
    delete ui;
}

QString CampaignForm::campaignName() const
{
    if (!m_campaign)
    {
        return tr("[Invalid]");
    }

    return m_campaign->name();
}

bool CampaignForm::campaignChanged() const
{
    if (!m_campaign)
    {
        return false;
    }

    return m_campaign->changed();
}

bool CampaignForm::onSave(const QString &fileName)
{
    if (fileName.isEmpty())
    {
        return FileActions::save(m_campaign, m_fileName);
    }

    if (!FileActions::save(m_campaign, fileName))
    {
        return false;
    }

    m_fileName = fileName;
    return true;


}


void CampaignForm::on_actionAddSkill_triggered()
{

//    m_model->insertRows(0,1,)
}

void CampaignForm::on_actionAddSpecialAbility_triggered()
{

}

void CampaignForm::on_actionAddCharacterClass_triggered()
{

}
